﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexySys.Common.Attributes
{
    #region CommonControl

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class CommonControlAttribute : Attribute
    {
        public string Type { get; set; }
        public string Path { get; set; }
        public string Properties { get; set; }

        public CommonControlAttribute(string type, string path, string properties)
        {
            Type = type;
            Path = path;
            Properties = properties;
            if(!properties.Split(',').Contains("ID"))
            {
                if (Properties != "")
                    Properties += ",ID";
                else
                    Properties = "ID";
            }
        }
    }

    #endregion

    #region Converters

    public class IntArrayConverterAttribute : System.ComponentModel.TypeConverter
    {
        public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }

        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string val = value as string;
            string[] vals = val.Split(',');
            System.Collections.Generic.List<int> ints = new System.Collections.Generic.List<int>();
            foreach (string s in vals)
                ints.Add(Convert.ToInt32(s));
            return ints.ToArray();
        }
    }

    #endregion
}
